package com.ztz.mynewdemo.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.bean.TodayResponse;
import com.ztz.mynewdemo.utils.NewsApi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateActivity extends BaseActivity {
    DatePicker datePicker;
    int year, month, day;
    Calendar calendar;
    TextView tv,tv1;
    List<TodayResponse.ResultBean> resultBeanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);
        long time=System.currentTimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=new Date(time);
        String t1=format.format(d1);
        Log.d("msg", t1);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        tv=(TextView)findViewById(R.id.tv_date);
        tv1=(TextView)findViewById(R.id.date_tv);
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-" + monthOfYear + "-" + dayOfMonth);
                showDate(year,monthOfYear+1,dayOfMonth);

            }
        });
        showDate(year,month+1,day);
        showDate1(year,month+1,day);
        getData();
        datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DateActivity.this,TodayHistoryActivcity.class);
                startActivity(intent);
            }
        });
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                setTitle(year+"-"+month+"-"+dayOfMonth);
                showDate1(year,month+1,dayOfMonth);
            }
        },year,month,day).show();
    }

    private void showDate1(int year, int month, int dayOfMonth) {
        tv1.setText("日期: "+year+"年"+month+"月"+dayOfMonth+"日");
    }

    private void showDate(int year, int monthOfYear, int dayOfMonth) {
        tv.setText("日期 : "+year+"年"+monthOfYear+"月"+dayOfMonth+"日");
    }

    private void getData() {
        OkGo.<String>get(NewsApi.TODAY)
                .tag(this)
                .params("key", "907298e415a8318e78b1cb4a3403c01e")
                .params("v", 1.0)
                .params("month", month+1)
                .params("day", day)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }
                });


    }
}